const got = require('got');

const custom = got.extend({
  retry: 2,
  hooks: {
    beforeRetry: [
      (options, err, count) => {
        console.error(`Request ${options.url} fail, retry attempt #${count}: ${err}`);
      },
    ],
    afterResponse: [
      (response) => {
        try {
          response.data = JSON.parse(response.body);
        } catch (e) {
          response.data = response.body;
        }
        response.status = response.statusCode;
        return response;
      },
    ],
    init: [
      (options) => {
        // compatible with axios api
        if (options && options.data) {
          options.body = options.body || options.data;
        }
      },
    ],
  },
  headers: {
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36',
    strictSSL: false,
    rejectUnauthorized: false
  },
  timeout: 30000,
});
custom.all = (list) => Promise.all(list);

module.exports = custom;
